// pages/newaddress/newaddress.js
const app = getApp()

import adres from '../../utils/address'
import Toast from '../../miniprogram/@vant/weapp/dist/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      name: '',
      mobile: '',
      detail: '',
      area: [],
      openid: '',
    },
    show: false,
    areaList: adres,
    updataId: 0
  },
  addsave() {
    let url = "http://localhost:8033/address/saveAddress"
    let params = {
      name: this.data.form.name,
      mobile: this.data.form.mobile,
      detail: this.data.form.detail,
      area: JSON.stringify(this.data.form.area),
      openid: this.data.form.openid
    }
    if (this.data.updataId) {
      url = 'http://localhost:8033/address/updataAddress';
      params = {
        id: this.data.updataId,
        name: this.data.form.name,
        mobile: this.data.form.mobile,
        detail: this.data.form.detail,
        area: JSON.stringify(this.data.form.area),
      }
    }
    wx.request({
      url: url,
      method: 'POST',
      data: params,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: res => {
        if (this.data.updataId) {
          Toast.success('修改成功');
        } else {
          Toast.success('新增成功');
        }
      }
    })
  },
  region(values) {
    let arr = []
    for (var i = 0; i < values.detail.values.length; i++) {
      console.log(values.detail.values[i])
      arr.push(values.detail.values[i].name)
      this.setData({
        area: arr
      })
      this.data.form.area = arr
    }
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  onChangeregion(event) {
    this.setData({
      show: true
    });
    console.log(1)
  },
  onChangenaddress(event) {
    console.log(event.detail)
    this.data.form.detail = event.detail
  },
  onChangename(event) {
    // event.detail 为当前输入的值
    this.data.form.name = event.detail
    console.log(event.detail);
  },
  onChangenphone(event) {
    console.log(event.detail)
    this.data.form.mobile = event.detail
  },
  getItem() {
    wx.request({
      url: 'http://localhost:8033/address/getAddressItem',
      data: {
        id: this.data.updataId
      },
      success: res => {
        let item = {
          ...res.data.data[0]
        }
        item.area = JSON.parse(item.area)
        this.setData({
          form: item,
          area: item.area
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      updataId: options.sid
    })
    this.data.form.openid = app.globalData.openid
    if (options.sid) {
      this.getItem()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})